##PBAR_Zspec_CompareSimulatedReal.py
import numpy as np
##parameters
detectorNo = 68

datasetNo = datasetGroupsIndices['Pb'][-1]
datasetNo = datasetGroupsIndices['Al'][-1]

acquisitionTime = datasetAcquisitionTime[datasetNo]
totalCounts = dat[datasetNo][:,detectorNo].sum().astype(float)
rate = totalCounts/acquisitionTime

acquisitionTimeMC = 1.0
numberCountsMC = totalCounts * acquisitionTimeMC / acquisitionTime / 2.0

probdist = dat[datasetNo][:,detectorNo].astype(float) / dat[datasetNo][:,detectorNo].sum()
bins = np.random.choice(np.arange(256), size=numberCountsMC, replace=True, p=probdist)

##make histogram
binEdges = np.arange(257)

counts, binEdges = np.histogram(bins, bins = binEdges)
binCenters = (binEdges[1:]+ binEdges[:-1])/2

# plot histogram
figure()
##hist(bins, bins = range(256))
plot(binCenters, counts, label = 'Simulated, 30 Hz, 1 sec')
plot(dat[datasetNo][:,detectorNo]/(acquisitionTime*2) * acquisitionTimeMC, label= 'High Stat, Scaled')

# plot LS data
ind = datasetGroupsIndices['PbLS'][-1]
ind = datasetGroupsIndices['AlLS'][-1]
plot(dat[ind][:,detectorNo]/2, label = 'Low Stat 2 sec, Scaled')

plt.yscale('log')
plt.xlabel('Bin')
plt.ylabel('Counts')

plt.grid()
plt.xlim((0, 100))
plt.legend( loc = 1)

# histogram the bins
binEdges32 = np.arange(6.0, 105, 3)
binCenters32 = (binEdges32[1:]+ binEdges32[:-1])/2
